JCAT.Class = function()
{
}

JCAT.Class.CreateAbstractMethod = function (sFullMethodName)
{
	sCode = sFullMethodName + " = function ()\r\n" ;
	sCode+= "{var what_this='--this abstract a method--' ;\r\n" ;
	sCode+= "	throw new Error( JCAT.Language.Sentence('错误：正在访问一个抽象方法 %s。','JCAT.JS',null,'"+sFullMethodName+"') ) ;\r\n" ;
	sCode+= "}\r\n" ;

	eval(sCode) ;
}

JCAT.Class.CreateAbstractClass = function (sClassName)
{
	sCode = sClassName + " = function ()\r\n" ;
	sCode+= "{\r\n" ;
	sCode+= "	throw new Error( JCAT.Language.Sentence('错误：正在实例化一个抽象类 %s。','JCAT.JS',null,'"+sClassName+"') ) ;\r\n" ;
	sCode+= "}\r\n" ;

	eval(sCode) ;
}

JCAT.Class.IsAbstractMethod = function (Method)
{
	if( typeof(Method)!='function' )
	{
		throw new Error(
			JCAT.Language.Sentence('参数 Method 必须为类(function); 传入类型为:%s','JCAT.JS',null,typeof(Method))
		) ;
	}
	
	sMethodCode = Method.toString() ;
	return sMethodCode.search(/--this abstract a method--/i)>=0 ;
}

JCAT.Class.AssertConcreteClass = function (sClassName)
{
	if( typeof(sClassName)!='string' )
	{
		throw new Error(
			JCAT.Language.Sentence('参数 sClassName 必须为类名称(字符串); 传入类型为:%s','JCAT.JS',null,typeof(sClassName))
		) ;
	}
	
	eval( 'Class =' + sClassName ) ;
	var nAbstractMethodCount = 0 ;
	var sAbstractMethods = '' ;
	
	// 历遍所有的方法,检查是否存在 抽象方法
	for( var sMemberName in Class.prototype )
	{
		eval('Method = Class.prototype.'+sMemberName) ;
		if( JCAT.Class.IsAbstractMethod(Method) )
		{
			nAbstractMethodCount ++ ;
			
			if(sAbstractMethods)
			{
				sAbstractMethods+= ', ' ;
			}
			sAbstractMethods+= sMemberName ;
		}
	}
	
	if(nAbstractMethodCount)
	{
		throw new Error(
			JCAT.Language.Sentence('抽象类%s 无法实例化; 以下抽象方法尚未实现:%s','JCAT.JS',null,sClassName,sAbstractMethods)
		) ;
	}
}

JCAT.Class.AssertHasImplements = function(aObject,sInterfaceName)
{
	// 检查参数 
	if( typeof(aObject)!='object' )
	{
		throw new Error(
			JCAT.Language.Sentence('参数 aObject 必须为对象.','JCAT.JS',null)
		) ;		
	}
	if( typeof(sInterfaceName)!='string' )
	{
		throw new Error(
			JCAT.Language.Sentence('参数 sInterfaceName 必须有效的接口名称.','JCAT.JS',null)
		) ;		
	}
	
	eval( 'Interface = ' + sInterfaceName ) ;
	if( typeof(Interface)!='function' )
	{
		throw new Error(
			JCAT.Language.Sentence('参数 sInterfaceName 必须有效的接口名称.','JCAT.JS',null,sInterfaceName)
		) ;	
	}
	
	if( !JCAT.Class.HasImplements(aObject,Interface) )
	{
		throw new Error(
			JCAT.Language.Sentence('对象必须实现接口 %s ','JCAT.JS',null)
		) ;			
	}
}

JCAT.Class.HasImplements = function ( ClassOrIns, Interface, bThrowException/*=false*/ )
{
	if( typeof(bThrowException)!='function' )
	{
		bThrowException = false ;
	}
	
	if( typeof(Interface)!='function' )
	{
		throw new Error('Parameter Interface must be a Class(function).') ;
	}
	
	if( typeof(ClassOrIns)=='function' )
	{
		var bClass = true ;
	}
	else if( typeof(ClassOrIns)=='object' )
	{
		var bClass = false ;
	}
	else
	{
		throw new Error('Parameter ClassOrIns must be a Class(function) or a instance.') ;
	}

	for(var InterfaceMemberName in Interface.prototype)
	{
		eval("bIsFunction = (typeof(Interface.prototype."+InterfaceMemberName+")=='function')") ;
		if(!bIsFunction)
		{
			continue ;
		}
		
		// 类
		if( bClass )
		{
			MemberName = 'ClassOrIns.prototype.' + InterfaceMemberName ;
		}
		
		// 对象
		else
		{
			MemberName = 'ClassOrIns.' + InterfaceMemberName ;
		}
		
		eval("bImplementsFunction = (typeof("+MemberName+")=='function')") ;
		if(!bImplementsFunction)
		{
			if(bThrowException)
			{
				throw new Error(
					JCAT.Language.Sentence('Parameter ClassOrIns hasn`t implements Interface method: %s.','JCAT.JS',null,InterfaceMemberName) ) ;
			}
			
			else
			{
				return false ;
			}
		}
	}
	
	return true ;
}

JCAT.Class.Inherit = function ( SubClass, ParentClass, sParentClassConstructrName/*=null*/ )
{
	if( typeof(SubClass)!='function' )
	{
		throw new Error('Parameter SubClass must be a Class(function).') ;
	}
	if( typeof(ParentClass)!='function' )
	{
		throw new Error('Parameter ParentClass must be a Class(function).') ;
	}
	
	// 拷贝父类 的构造函数
	if( typeof(sParentClassConstructrName)=='string' )
	{
		eval("SubClass.prototype."+sParentClassConstructrName+" = ParentClass") ;
	}
	
	// 将 父类 的方法拷贝给 子类
	for(var MemberName in ParentClass.prototype)
	{		
		// 重载
		eval("bMethodExtents = (typeof(SubClass.prototype."+MemberName+")=='function')")
		if(bMethodExtents)
		{
			throw new Error('SubClass has method: '+MemberName+' already, Can not inherit from ParentClass.') ;
		}
		
		// 拷贝
		eval("SubClass.prototype."+MemberName+" = ParentClass.prototype."+MemberName) ;		
	}
}

JCAT.Class.CheckDependClasses = function (arrClasses,sForWhom/*=null*/,bThrowException/*=true*/)
{
	if( typeof(bThrowException)!='function' )
	{
		bThrowException = true ;
	}
	if( typeof(sForWhom)=='undefined' )
	{
		sForWhom = '' ;
	}
	
	for(var nIdx=0; nIdx<arrClasses.length; nIdx++)
	{
		eval( "bExists = (typeof("+arrClasses[nIdx]+")=='function')" )
		if(!bExists)
		{
			if(bThrowException)
			{
				throw new Error(
					JCAT.Language.Sentence('%s 缺少依赖的类 %s 。','JCAT.JS',null
					, sForWhom, arrClasses[nIdx] )) ;
			}
			
			else
			{
				return false ;
			}
		}
	}
}
